package com.welyo.view.quizz {
	import com.welyo.model.quizz.TabItemVO;
	import com.welyo.model.QuizzModel;
	import com.welyo.view.quizz.button.TabButton;
	import com.welyo.view.quizz.event.QuizzEvent;
	import com.welyo.view.quizz.instruction.Instruction;
	import com.welyo.view.quizz.testwith.BaseTestWith;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author vu.doan
	 */
	public class QuizzContent extends Sprite {
		
		private var arrButtonTab		:Array;
		private var arrTabContent		:Array;
		
		private var wrongSound			:Sound;
		private var correctSound		:Sound;
		
		private var soundNormal			:Sound;
		private var soundLast30			:Sound;
		private var sNormal				:SoundChannel;
		private var sLast30				:SoundChannel;
		
		public function QuizzContent() {
			arrButtonTab = [mcGame.mcButtonHolder.mcButtonText, mcGame.mcButtonHolder.mcButtonNumber];
			arrTabContent = [mcGame.mcTestWithText, mcGame.mcTestWithNumber];
			for (var i:int = 0; i < arrTabContent.length; i++) {
				(arrTabContent[i] as BaseTestWith).hide();
			}
			
			wrongSound = new WrongSound();
			correctSound = new CorrectSound();
			
			soundNormal = new SoundNormal();
			soundLast30 = new Sound30Last();
			
			show();
			
			mcGame.startgame.buttonMode = true;
			mcGame.startgame.mcStart.mcLight.mouseEnabled = false;
			mcGame.startgame.addEventListener(MouseEvent.CLICK, onClickStartGameHandler);
			mcGame.startgame.addEventListener(MouseEvent.MOUSE_OVER, onOverStartGameHandler);
			
			mcGame.mcResult.mcReplay.buttonMode = true;
			mcGame.mcResult.mcReplay.visible = false;
			mcGame.mcResult.mcReplay.addEventListener(MouseEvent.CLICK, onClickReplayGameHandler);
			mcGame.mcResult.txtResult.autoSize = TextFieldAutoSize.LEFT;
			mcGame.mcResult.txtResultShadow.autoSize = TextFieldAutoSize.LEFT;
			
			mcGame.mcPaper.addEventListener(QuizzEvent.DROP_COMPLETE, onDropPaperCompleteHandler);
			mcGame.mcInstruction.addEventListener(QuizzEvent.START_GAME, onStartGameHandler);
			
			mcGame.mcTopPlayer.addEventListener(QuizzEvent.GAME_REPLAY, onGameReplayHandler);
			mcGame.mcTopPlayer.addEventListener(QuizzEvent.SAVING_GAME, onSavingGameHandler);
			mcGame.mcTopPlayer.addEventListener(QuizzEvent.SAVE_COMPLETE, onSaveCompleteGameHandler);
			
			QuizzModel.getIntance().addEventListener(QuizzEvent.CHANGE_TAB, onChangeTabHandler);
			QuizzModel.getIntance().addEventListener(QuizzEvent.CHANGE_MARK, onChangeMarkHandler);
			QuizzModel.getIntance().addEventListener(QuizzEvent.COMPLETE_QUIZZ, onCompleteQuizzHandler);
			QuizzModel.getIntance().addEventListener(QuizzEvent.WRONG_ANSWER, onWrongAnswerHandler);
			QuizzModel.getIntance().addEventListener(QuizzEvent.CORRECT_ANSWER, onCorrectAnswer);
			QuizzModel.getIntance().addEventListener(QuizzEvent.START_LAST_30, onLast30SecondHandler);
			
			this.mouseChildren = this.mouseEnabled = false;
			mcSaving.visible = false;
			
			// test
			//init();
		}
		
		private function onClickReplayGameHandler(evt:MouseEvent):void {
			onGameReplayHandler(null);
		}
		
		private function onSavingGameHandler(evt:QuizzEvent):void {
			mcSaving.visible = true;
		}
		
		private function onSaveCompleteGameHandler(evt:QuizzEvent):void {
			mcSaving.visible = false;
		}
		
		private function onGameReplayHandler(evt:QuizzEvent):void {
			QuizzModel.getIntance().resetDataGame();
			show();
		}
		
		private function onWrongAnswerHandler(evt:QuizzEvent):void {
			wrongSound.play();
		}
		
		private function onCorrectAnswer(evt:QuizzEvent):void {
			correctSound.play();
		}
		
		private function onLast30SecondHandler(evt:QuizzEvent):void {
			sLast30 = soundLast30.play(0, 20);
			if (sNormal) {
				sNormal.stop();
			}
		}
		
		public function init(path:String = "../xml/quizz.xml"):void {
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadDataHandler);
			ldr.addEventListener(IOErrorEvent.IO_ERROR, onErrorLoadDataHandler);
			
			ldr.load(new URLRequest(path));
		}
		
		private function onErrorLoadDataHandler(evt:IOErrorEvent):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadDataHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorLoadDataHandler);
		}
		
		private function onCompleteLoadDataHandler(evt:Event):void {
			this.mouseChildren = this.mouseEnabled = true;
			
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadDataHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorLoadDataHandler);
			
			var data:XML = new XML(evt.currentTarget.data);
			QuizzModel.getIntance().parseData(data);
			
			(mcGame.mcCountDown as CountDownDisplay).init();
			
			// set label for buttons
			for (var i:int = 0; i < arrButtonTab.length; i++) {
				initButton(arrButtonTab[i] as MovieClip, i);
			}
		}
		
		public function show():void {
			mcGame.startgame.visible = true;
			mcGame.mcHat.visible = mcGame.mcElephant.visible = true;
			mcGame.mcSlogan.visible = true;
			
			mcGame.mcNotebook.visible = false;
			mcGame.mcInstruction.visible = false;
			mcGame.mcButtonHolder.visible = false;
			mcGame.mcCountDown.visible = false;
			mcGame.mcMark.visible = false;
			mcGame.mcResult.visible = false;
			mcGame.mcMedal.visible = false;
			mcGame.mcTopPlayer.hide();
			mcSaving.visible = false;
			
			for (var i:int = 0; i < arrTabContent.length; i++) {
				(arrTabContent[i] as BaseTestWith).hide();
			}
		}
		
		private function startGame():void {
			mcGame.startgame.visible = false;
			mcGame.mcHat.visible = mcGame.mcElephant.visible = false;
			mcGame.mcSlogan.visible = false;
			
			mcGame.mcNotebook.visible = true;
			mcGame.mcInstruction.visible = true;
			QuizzModel.getIntance().TotalMark = 0;
		}
		
		private function onClickStartGameHandler(evt:MouseEvent):void {
			trackingQuizz();
			
			QuizzModel.getIntance().TimeRemain = QuizzModel.getIntance().TotalTime;
			startGame();
			QuizzModel.getIntance().CurrentTab = 0;
		}
		
		private function onOverStartGameHandler(evt:MouseEvent):void {
			mcGame.startgame.mcStart.gotoAndPlay("over");
		}
		
		private function initButton(mc:MovieClip, index:int):void {
			var vo:TabItemVO = QuizzModel.getIntance().getTabAtIndex(index);
			(mc as TabButton).Label = vo.ButtonLabel;
			mc.addEventListener(MouseEvent.CLICK, onClickButtonTabHandler);
		}
		
		private function onChangeTabHandler(evt:QuizzEvent):void {
			mcGame.mcButtonHolder.visible = true;
			mcGame.mcInstruction.visible = true;
			QuizzModel.getIntance().stopCountDown();
			if (sNormal) {
				sNormal.stop();
			}
			
			if (sLast30) {
				sLast30.stop();
			}
			
			if (QuizzModel.getIntance().TotalComplete == 0) {
				mcGame.mcButtonHolder.mouseChildren = mcGame.mcButtonHolder.mouseEnabled = true;
			}
			
			var index:int = QuizzModel.getIntance().CurrentTab;
			var vo:TabItemVO = QuizzModel.getIntance().getTabAtIndex(index);
			if (vo) {
				(mcGame.mcInstruction as Instruction).setInstruction(vo.Title, vo.Description, vo.StartTitle);
			}
			
			for (var i:int = 0; i < arrButtonTab.length; i++) {
				(arrButtonTab[i] as TabButton).Active = false;
			}
			
			(arrButtonTab[index] as TabButton).Active = true;
		}
		
		private function onClickButtonTabHandler(evt:MouseEvent):void {
			var index:int = arrButtonTab.indexOf(evt.currentTarget);
			
			if (arrButtonTab[QuizzModel.getIntance().CurrentTab]) {
				(arrButtonTab[QuizzModel.getIntance().CurrentTab] as TabButton).Active = false;
			}
			QuizzModel.getIntance().CurrentTab = index;
		}
		
		private function onStartGameHandler(evt:QuizzEvent):void {
			var index:int = QuizzModel.getIntance().CurrentTab;
			
			(mcGame.mcCountDown as CountDownDisplay).show();
			mcGame.mcMark.visible = true;
			mcGame.startgame.visible = false;
			mcGame.mcNotebook.gotoAndStop("clear");
			mcGame.mcInstruction.visible = false;
			mcGame.mcButtonHolder.mouseChildren = mcGame.mcButtonHolder.mouseEnabled = false;
			
			QuizzModel.getIntance().startCountDown();
			
			if (sLast30) {
				sLast30.stop();
			}
			
			if (sNormal) {
				sNormal.stop();
			}
			sNormal = soundNormal.play(0, 100);
			
			if (arrTabContent[index]) {
				(arrTabContent[index] as BaseTestWith).show();
				(arrTabContent[index] as BaseTestWith).start(QuizzModel.getIntance().getTabAtIndex(index));
			}
		}
		
		private function trackingQuizz(index:int = -1):void {
			var request:URLRequest = new URLRequest("tracking.php");
			var param:URLVariables = new URLVariables();
			
			param.gameid = "quizz";
			param.value = 1;
			request.data = param;
			request.method = URLRequestMethod.POST;
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteTrackingHandler);
			ldr.addEventListener(IOErrorEvent.IO_ERROR, onErrorTrackingHandler);
			ldr.load(request);
		}
		
		private function onErrorTrackingHandler(evt:IOErrorEvent):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteTrackingHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorTrackingHandler);
		}
		
		private function onCompleteTrackingHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteTrackingHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorTrackingHandler);
		}
		
		private function onChangeMarkHandler(evt:QuizzEvent):void {
			mcGame.mcMark.txtMark.text = QuizzModel.getIntance().TotalMark;
			mcGame.mcMark.txtMarkShadow.text = QuizzModel.getIntance().TotalMark;
		}
		
		private function onCompleteQuizzHandler(evt:QuizzEvent):void {
			mcGame.mcResult.visible = true;
			QuizzModel.getIntance().stopCountDown();
			if (sNormal) {
				sNormal.stop();
			}
			
			if (sLast30) {
				sLast30.stop();
			}
			mcGame.mcCountDown.visible = mcGame.mcMark.visible = false;
			
			for (var i:int = 0; i < arrButtonTab.length; i++) {
				(arrButtonTab[i] as TabButton).Active = false;
				(arrTabContent[i] as BaseTestWith).hide();
			}
			
			if (QuizzModel.getIntance().IsWin) {
				mcGame.mcResult.txtResult.htmlText = QuizzModel.getIntance().WinLabel;
				mcGame.mcResult.txtResultShadow.htmlText = QuizzModel.getIntance().WinLabel;
				mcGame.mcResult.gotoAndStop("win");
				mcGame.mcPaper.show();	
				mcGame.mcResult.mcReplay.visible = false;
			} else {
				mcGame.mcResult.txtResult.htmlText = QuizzModel.getIntance().LoseLabel;
				mcGame.mcResult.txtResultShadow.htmlText = QuizzModel.getIntance().LoseLabel;
				mcGame.mcResult.gotoAndStop("lose");
				mcGame.mcResult.mcReplay.visible = true;
			}
		}
		
		private function onDropPaperCompleteHandler(evt:QuizzEvent):void {
			mcGame.mcTopPlayer.init();
			mcGame.mcTopPlayer.show();
			mcGame.mcResult.visible = false;
			if (QuizzModel.getIntance().IsWin) {
				mcGame.mcMedal.visible = true;
			} else {
				mcGame.mcMedal.visible = false;
			}
		}
		
		public function dispose():void {
			QuizzModel.getIntance().stopCountDown();
			if (sNormal) {
				sNormal.stop();
			}
			
			if (sLast30) {
				sLast30.stop();
			}
		}
	}
}